package com.unhz.taigu.service.impl;

import com.unhz.generator.dao.InsoleImageMapper;
import com.unhz.generator.enums.EnumValid;
import com.unhz.generator.pojo.InsoleImage;
import com.unhz.taigu.service.InsoleImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Author: 王林恩
 * @Description:
 * @Date: Created in 下午 2:26 2018/1/30 0030
 * @Modified By:
 */


@Service

public class InsoleImageServiceImpl implements InsoleImageService{

    @Autowired
    private InsoleImageMapper insoleImageMapper;

    @Override
    public InsoleImage selectOne() {
        //insoleImageMapper.selectByPrimaryKey();
        return null;
    }

    @Override
    public int addInsoleImage(InsoleImage insoleImage) {
        int res = insoleImageMapper.insert(insoleImage);
        return res;
    }

    public void insertInsoleImage(Integer insoleId, MultipartFile[] insoleImg, HttpServletRequest request){
        //保存中底图片信息
        File targetFile=null;
        String msg="";//返回存储路径
        int code=1;
        List imgList=new ArrayList();
        InsoleImage insoleImage = null;
        if (insoleImg!=null && insoleImg.length>0) {
            for (int i = 0; i < insoleImg.length; i++) {
                String fileName=insoleImg[i].getOriginalFilename();//获取文件名加后缀
                if(fileName!=null&&fileName!=""){
                    String returnUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() +"/upload/imgs/";//存储路径
                    String path = request.getSession().getServletContext().getRealPath("upload/imgs"); //文件存储位置
                    String fileF = fileName.substring(fileName.lastIndexOf("."), fileName.length());//文件后缀
                    fileName=new Date().getTime()+"_"+new Random().nextInt(1000)+fileF;//新的文件名
                    //先判断文件是否存在
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                    String fileAdd = sdf.format(new Date());
//                    String fileAdd = DateUtil.format(new Date(),"yyyyMMdd");
                    File file1 =new File(path+"/"+fileAdd);
                    //如果文件夹不存在则创建
                    if(!file1.exists()  && !file1.isDirectory()){
                        file1.mkdirs();
                    }
                    targetFile = new File(file1, fileName);
                    try {
                        insoleImg[i].transferTo(targetFile);
                        msg=returnUrl+fileAdd+"/"+fileName;
                        insoleImage = new InsoleImage();
                        insoleImage.setInsoleId(insoleId);
                        insoleImage.setImageUrl(msg);
                        insoleImage.setCreateTime(new Date());
                        insoleImage.setValid(EnumValid.ENABLED.getIndex());
                        insoleImageMapper.insertSelective(insoleImage);
//                        imgList.add(msg);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
//        return JSON.toJSONString(imgList);
    }

    @Override
    public int insertSelective(InsoleImage pojo) {

        int flag = insoleImageMapper.insertSelective(pojo);

        return flag;
    }

    @Override
    public void deleteInsoleImage(Integer insoleId) {
        insoleImageMapper.deleteInsoleImage(insoleId);
    }

    public List<InsoleImage> selectInsoleImageList(Integer insoleId) {
        return insoleImageMapper.selectInsoleImageList(insoleId);

    }


}

